package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.DefinitionDetail;
import com.ruoyi.business.domain.DefinitionInfo;
import com.ruoyi.business.mapper.DefinitionDetailMapper;
import com.ruoyi.business.service.IDefinitionDetailService;
import com.ruoyi.business.service.IprocessService;
import com.ruoyi.common.core.text.Convert;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.util.List;

/**
 * 流程定义明细Service业务层处理
 *
 * @author x1ao
 * @date 2021-07-19
 */
@Service
public class DefinitionDetailServiceImpl implements IDefinitionDetailService {
    @Autowired
    private DefinitionDetailMapper definitionDetailMapper;

    @Autowired
    private IprocessService processService;

    /**
     * 查询流程定义明细
     *
     * @param definitionInfoId 流程定义明细ID
     * @return 流程定义明细
     */
    @Override
    public DefinitionDetail selectDefinitionDetailById(Long definitionInfoId) {
        return definitionDetailMapper.selectDefinitionDetailById(definitionInfoId);
    }

    /**
     * 查询流程定义明细列表
     *
     * @param definitionDetail 流程定义明细
     * @return 流程定义明细
     */
    @Override
    public List<DefinitionDetail> selectDefinitionDetailList(DefinitionDetail definitionDetail) {
        List<DefinitionDetail> definitionDetailList = definitionDetailMapper.selectDefinitionDetailList(definitionDetail);
        for (DefinitionDetail detail : definitionDetailList) {
            ProcessDefinition processDefinition = processService.getProcessDefinitionByDeploymentId(detail.getDeploymentId());
            detail.setDeployKey(processDefinition.getKey());
            detail.setVersion(processDefinition.getVersion());
            detail.setDescription(processDefinition.getDescription());
        }
        return definitionDetailList;
    }

    /**
     * 新增流程定义明细
     *
     * @param definitionDetail 流程定义明细
     * @return 结果
     */
    @Override
    public int insertDefinitionDetail(DefinitionDetail definitionDetail) {
        return definitionDetailMapper.insertDefinitionDetail(definitionDetail);
    }

    /**
     * 修改流程定义明细
     *
     * @param definitionDetail 流程定义明细
     * @return 结果
     */
    @Override
    public int updateDefinitionDetail(DefinitionDetail definitionDetail) {
        return definitionDetailMapper.updateDefinitionDetail(definitionDetail);
    }

    /**
     * 删除流程定义明细对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDefinitionDetailByIds(String ids) {
        return definitionDetailMapper.deleteDefinitionDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除流程定义明细信息
     *
     * @param definitionInfoId 流程定义明细ID
     * @return 结果
     */
    @Override
    public int deleteDefinitionDetailById(Long definitionInfoId) {
        return definitionDetailMapper.deleteDefinitionDetailById(definitionInfoId);
    }

    @Override
    @Transactional
    public void deploy(Long definitionId, String filePath) throws FileNotFoundException {
        //把文件部署到Activiti7中,得到Deployment对象
        Deployment deployment = processService.deploy(filePath);
        //封装DefinitionDetail对象,把数据存储到数据库中
        //DefinitionDetail 部署后要查出来的封装对象
        DefinitionDetail detail = new DefinitionDetail();
        //封装DefinitionInfo对象,把传入的id设置到对象中
        DefinitionInfo info = new DefinitionInfo();
        info.setId(definitionId);
        //流程定义对象
        detail.setDefinitionInfo(info);
        //关联Activiti7中的部署Id
        detail.setDeploymentId(deployment.getId());
        definitionDetailMapper.insertDefinitionDetail(detail);
    }
}
